﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace shuju_jiange
{
    public partial class FrmShowTotal : Form
    {
        private static string[] searchTexts;
        private bool isShowAll = true;
        public FrmShowTotal()
        {
            InitializeComponent();
            if(FrmShowTotal.searchTexts != null && FrmShowTotal.searchTexts.Length>0)
            {
                foreach(string s in FrmShowTotal.searchTexts)
                {
                    this.tbSearchText.Text += " " + s;
                }
            }
        }

        public Dictionary<string, double> Data { get; } = new Dictionary<string, double>();

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            ShowChart();
        }

        private void btnShowAll_Click(object sender, EventArgs e)
        {
            isShowAll = !isShowAll;
            this.btnShowAll.Text = isShowAll ? "显示查找数据" : "显示全部";
            ShowChart();
        }

        private void FrmShowTotal_Load(object sender, EventArgs e)
        {
            this.btnShowAll.Text = isShowAll ? "显示查找数据" : "显示全部";
            ShowChart();
        }
        private void ShowChart()
        {
            this.chartData.Series[0].Points.Clear();
            chartData.ChartAreas[0].CursorX.IsUserEnabled = true;
            chartData.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            chartData.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            // chartData.ChartAreas[0].AxisX.Maximum = 520;
            chartData.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
            // chartData.ChartAreas[0].AxisX.ScaleView.Size=20;
            chartData.ChartAreas[0].AxisX.IntervalAutoMode= IntervalAutoMode.VariableCount;
            int v = 0;
            // 获取查询值
            string[] searchTexts = this.tbSearchText.Text.Trim().Split(' ');  
            for(int i = 0; i< searchTexts.Length; i++)
            {
                searchTexts[i] = searchTexts[i].Trim();
            }
            FrmShowTotal.searchTexts = searchTexts;
            foreach (KeyValuePair<string, double> m in Data)
            {
                v++;
                if (isShowAll)
                {
                    DataPoint point = this.chartData.Series[0].Points.Add(m.Value);
                    point.AxisLabel = v.ToString() + "(" + m.Key + ")";
                    point.XValue = v;
                    if (searchTexts.Contains(m.Key))
                    {
                            point.Color = Color.Red;
                    };
                }
                else
                {
                    if (searchTexts.Contains(m.Key))
                    {
                        DataPoint point = this.chartData.Series[0].Points.Add(m.Value);
                        point.AxisLabel = v.ToString() + "(" + m.Key + ")";
                        point.Color = Color.Red;
                        point.IsValueShownAsLabel = true;
                    };
                }
            }
        }
    }
}
